package com.flycms.web.system.rest;

import com.alibaba.druid.util.StringUtils;
import com.flycms.core.base.BaseController;
import com.flycms.core.entity.DataVo;
import com.flycms.core.entity.PageVo;
import com.flycms.core.entity.RestDataVo;
import com.flycms.module.user.model.User;
import com.flycms.module.user.model.UserAdminVo;
import com.flycms.module.user.service.UserGroupService;
import com.flycms.module.user.service.UserPermissionService;
import com.flycms.module.user.service.UserRestService;
import com.flycms.utils.ConvertUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 开发公司：宏石<br/>
 * 版权：宏石<br/>
 *
 * @author sun-kaifei
 * @version 1.0 <br/>
 * @email xxx@qq.com
 * @Date: 2020-7-4
 */
@Controller
@RequestMapping("rest/system/user")
@Slf4j
public class UserAdminRestController extends BaseController {
    @Autowired
    protected UserRestService userService;
    @Autowired
    protected UserGroupService userGroupService;
    @Autowired
    protected UserPermissionService userPermissionService;

    /**
     * 没有权限的跳转
     * @return
     */
    @GetMapping("/nullPermission")
    @ResponseBody
    public RestDataVo nullPermission(){
        RestDataVo dataVo = new RestDataVo();
        dataVo.setMsg("此用户没有访问权限");
        dataVo.setCode(403);
        return dataVo;
    }

    //同步所有权限
    @GetMapping("/permission_sync")
    @ResponseBody
    @Deprecated
    public DataVo getSyncAllPermission(){
        if(userPermissionService.getSyncAllPermission()){
            return DataVo.success("同步权限成功");
        }
        return DataVo.failure("同步权限失败");
    }



    /**
     * 用户列表
     * @param username
     * @param nickName
     * @param mobile
     * @param email
     * @param pageIndex
     * @param pageSize
     * @return
     */

    @GetMapping(value = "/list")
    @ResponseBody
    public RestDataVo userList(@RequestParam(value = "userName", required = false) String username,
                           @RequestParam(value = "nickName", required = false) String nickName,
                           @RequestParam(value = "mobile", required = false) String mobile,
                           @RequestParam(value = "email", required = false) String email,
                           @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        PageVo<User> pageVo=userService.getUserListPage(username, nickName, mobile, email,null,null,pageIndex,pageSize);
        List<UserAdminVo> list = ConvertUtils.sourceToTarget(pageVo.getList(),UserAdminVo.class);
        return RestDataVo.PAGE(list,pageIndex,pageSize,pageVo.getCount());
    }



    /**
     * 增加新用户信息
     * @param user
     * @param result
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public RestDataVo userAdd(@Valid User user, BindingResult result){
        RestDataVo data;
        try {
            if (result.hasErrors()) {
                List<ObjectError> list = result.getAllErrors();
                for (ObjectError error : list) {
                    return RestDataVo.ERROR(error.getDefaultMessage());
                }
                return null;
            }
            data = userService.adminAddUser(user);
        } catch (Exception e) {
            data = RestDataVo.ERROR(e.getMessage());
        }
        return data;
    }


    /**
     * 删除用户
     * @param id
     * @return
     */
    @PostMapping("/del")
    @ResponseBody
    public RestDataVo deleteRole(@RequestParam(value = "id") String id){
        RestDataVo data ;
        if("1".equals(id)){
            return RestDataVo.ERROR("超级管理员组不能删除");
        }
        try {
            data = userService.deleteUserById(Long.parseLong(id));
        }catch (Exception e){
            data = RestDataVo.ERROR("异常" + e.getMessage());
        }
        return data;
    }


    /**
     * 编辑用户状态
     * @param params
     * @return
     */
    @PostMapping(value = "/edit")
    @ResponseBody
    public RestDataVo userEdit(
            @RequestBody Map<String,Object> params){
        String id = String.valueOf(params.get("id"));
        String status =String.valueOf(params.get("status"));
        if(!StringUtils.isNumber(id)||!StringUtils.isNumber(status)){
            return RestDataVo.ERROR("参数错误");
        }
        User user=userService.findUserById(Long.parseLong(id),null);
        if(user==null){
            return RestDataVo.ERROR("用户不存在");
        }else{
            try {
                userService.updateUserStatus(Long.parseLong(id),status);
                return RestDataVo.OK("修改成功");
            }catch (Exception e){
                log.error("异常：" + e);
                return RestDataVo.ERROR("异常：" + e);
            }
        }

    }


}
